package com.example.map.rx1demo.demos;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.map.rx1demo.R;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;
/**
 * Created by qundui on 2017/6/1.
 */

public class TestPage extends Activity {
    private Button btn;
    private Observable<String> stringObservable;
    private Subscription subscribe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        btn=new Button(this);
        btn.setText("按钮");
        setContentView(R.layout.test);
        stringObservable = RxBus.get().register("gsc", String.class);
        stringObservable.subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i("gsc","I am from Rxbus send!!!!"+s);
            }
        });

//          //两秒钟之内只取一个点击事件，防抖操作
//        RxView.clicks(btn).throttleFirst(2,TimeUnit.SECONDS)
//                .
//                subscribe(new Action1<Void>() {
//            @Override
//            public void call(Void aVoid) {
//                Toast.makeText(TestPage.this,"按钮被点击了"+ Thread.currentThread().getName(),Toast.LENGTH_SHORT).show();
//
//            }
//        });

        printHello();

    }
public void sendByRxBus(View v){
    RxBus.get().post("gsc","i am send form RXbus");
}
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister("gsc",stringObservable);
        if (subscribe!=null){
            subscribe.unsubscribe();// 取消订阅者
        }
    }
public void merge(View v){
    List<String> data=new ArrayList<>();
    data.add("A");
    data.add("B");
    data.add("C");
    data.add("D");
    List<Integer> data2=new ArrayList<>();
    data2.add(1);
    data2.add(2);
    data2.add(300);
    Observable  o=Observable.merge(Observable.from(data2),Observable.from(data));
//    o.subscribe(new Subscriber() {
//        @Override
//        public void onCompleted() {
//
//        }
//
//        @Override
//        public void onError(Throwable e) {
//
//        }
//
//        @Override
//        public void onNext(Object o) {
//            boolean b = o instanceof Integer;
//            Log.i("gsc",""+b);
//        }
//
//
//
//    });
    o.subscribe(new Action1() {
        @Override
        public void call(Object o) {
            boolean b = o instanceof Integer;
            //System.out.println(Looper.myLooper()==Looper.getMainLooper());
            Log.i("gsc",""+b);
        }
 });

}
    public void scan(View view){
        Observable observable=Observable.just(1,2,3,56);
        // 累加必须三个数字
        observable.scan(new Func2<Integer,Integer,Integer>() {


            @Override
            public Integer call(Integer integer, Integer integer2) {
                return integer+integer2;
            }
        }).subscribe(new Action1() {
            @Override
            public void call(Object o) {
                System.out.println("----"+o.toString());
            }
        });


    }
    public void filter(View view){
        Observable.just(300,-2,90,89).filter(new Func1<Integer, Boolean>() {
            @Override
            public Boolean call(Integer integer) {
                return integer>100;
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println("---fliter--"+integer);
            }
        });

    }
    public void zip(View view){
        //必须同一类型合并数据,否则抛出类型转换异常
        String m1[]={"a","x","demo"};
       String i[]={"23","45","78","67"};
        Observable observable=Observable.from(m1);
        Observable observable1=Observable.from(i);
        Observable observable2=Observable.zip(observable, observable1, new Func2<String,String,String>() {

            @Override
            public String call(String s, String s2) {
                return s+s2;
            }
        });
        observable2.subscribe(new Action1() {
            @Override
            public void call(Object o) {
                System.out.println(o);
            }
        });

    }
    public void take(View view){
        /*
     take ：取前n个数据
    takeLast：取后n个数据
    first 只发送第一个数据
    last 只发送最后一个数据
    skip() 跳过前n个数据发送后面的数据
    skipLast() 跳过最后n个数据，发送前面的数据
         */
        Observable<Integer> just = Observable.just(1, 34, 56, 7, 8, 90, 89);
        just.skipLast(3).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println("--take--"+integer);
            }
        });


    }
    public void elementAtDemo(View view){
//        Observable.just("x","y","Z","W").elementAt(2).subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
//                System.out.println("--elementAt-"+s);
//            }
//        });
        Observable.just("x","101","345").startWith("3","4","700").elementAtOrDefault(5,"78").subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println("--elementAtOrDefault-"+s);
            }
        });

    }
    public void startWith(View view){
        List<String> mdatas=new ArrayList<>();
        mdatas.add("w");
        mdatas.add("L");
        mdatas.add("M");
        Observable.just("a","fdy","zwj","ysl","hh","hsf").startWith(Observable.from(mdatas)).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println("-----startwith--"+s);
            }
        });

    }
    public void Timer(View view){
        //默认在调度器执行延时可以设置消费者在主线程回调
        Observable.timer(2,TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                System.out.println("---Timer--"+aLong);
                System.out.println(Looper.myLooper()==Looper.getMainLooper());
            }
        });

    }
    public void interval(View view){
        final Observable<Long> interval = Observable.interval(3000, 3000, TimeUnit.MICROSECONDS);
        interval.subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                System.out.println("----interval----"+aLong);

            }
        });

    }
public void range(View view){
     subscribe = Observable.range(12, 3)
            .subscribe(new Action1<Integer>() {
                @Override
                public void call(Integer integer) {
                    System.out.println("---range---" + integer);
                }
            });

}
    /**
     * 过滤重复数据
     * @param view
     */
    public void distinct(View view){
        Observable.just("1","3","1","3","5","5","4","100").distinctUntilChanged().subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println("==distinct==="+s);
            }
        });

    }
    public void throttleFirst(View view){
        Observable.interval(1,TimeUnit.SECONDS).throttleFirst(3,TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                System.out.println("==throttleFirst==="+aLong);
            }
        });

    }
    public void Buffer(View view){
        List<Integer> nums=new ArrayList<>();
        for(int i=0;i<10;i++){
            nums.add(i);

        }
        //拆分数据源发射
        Observable.from(nums).buffer(3).subscribe(new Action1<List<Integer>>() {
            @Override
            public void call(List<Integer> integers) {
                System.out.println( "buffer---------------" );
               Observable.from(integers).subscribe(new Action1<Integer>() {
                   @Override
                   public void call(Integer integer) {
                       System.out.println("-- Buffer--"+integer);
                   }
               });
            }
        });

    }
    public void doOnNext(View view){
        Observable.just(1,3,5).doOnNext(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println("--doOnnext-");
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                System.out.println("--onNext-");
            }
        });

    }
    public void delay(View view){
        Observable observable=Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(Object o) {
               finish();
            }
        }).delay(2,TimeUnit.SECONDS);
                observable.subscribe(new Observer() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

            }
        });

//        Observable.just("1","2").delay(2,TimeUnit.SECONDS).subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
//                System.out.println("-----delay--"+s);
//            }
//        });

    }
    /**
     *
     */
    private void printHello() {
        // 创建一个观察者
       Observer<String> o=new Observer<String>() {
           @Override
           public void onCompleted() {
               Log.i("gsc","观察者==onCompleted()");
               System.out.println(Looper.myLooper()==Looper.getMainLooper());
           }

           @Override
           public void onError(Throwable e) {
               Log.i("gsc","观察者==onError"+e.getMessage());
               System.out.println(Looper.myLooper()==Looper.getMainLooper());
           }

           @Override
           public void onNext(String s) {
               Log.i("gsc","观察者=="+s );
               System.out.println(Looper.myLooper()==Looper.getMainLooper());

           }
       };
//        //被观察者1.
//        Observable<String> ob=Observable.create(new Observable.OnSubscribe<String>() {
//            @Override
//            public void call(Subscriber<? super String> subscriber) {
//                System.out.println("I am 被观察者");
//
//               subscriber.onNext("A");
//                subscriber.onNext("B");
//            }
//        });
        //被观察者2.
        //Observable<String> ob=Observable.just("I am  被观察者","A","B");
       // 观察者3
        String[] words={"I am  被观察者","A","B"};
         Observable<String> ob=Observable.from(words);
     ob.subscribe(o);
        //支持回调
        //Scheduler 操作一下 subscribe  观察者的一个子类 并没有操作
//         ob.subscribe(new Action1<String>() {
//             @Override
//             public void call(String s) {
//               Log.i("gsc","ction1call"+s);//这个方法走
//             }
//         }, new Action1<Throwable>() {
//             @Override
//             public void call(Throwable throwable) {
//                 Log.i("gsc","Action1call2"+throwable.getMessage());
//             }
//         }, new Action0() {
//             @Override
//             public void call() {
//                 Log.i("gsc","--Action0call---");//这个方法走
//             }
//         });
        Observable.just("1","2","3")
                .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
                //.observeOn(AndroidSchedulers.mainThread()) //指定 subscribe()  回调到主线程
                .subscribe(new Action1<String>() {//如果不指定就在IO 子线程
            @Override
            public void call(String s) {

                System.out.println(Looper.myLooper()==Looper.getMainLooper());
                Log.i("gsc",s);
            }
        });
    }
}
